<?php

function error($message) {
	@fwrite(fopen('error_log', 'a'), date('Y-m-d h:i:s')."\t$message\n");
	die("<b>PHP Error: $message</b></br/>");
}

function mysql_log($resource_local, $query, $buffered = true)
{
	if (!$resource_local)
		return $resource_local;

        $resource = null;

	@fwrite(fopen('mysql_log.txt', 'a'), date('Y-m-d h:i:s')."\t$query\n");

        if ($buffered)
                $resource = mysql_query($query, $resource_local)
                        or die(mysql_error());
        else
                $resource = mysql_unbuffered_query($query, $resource_local)
                        or die(mysql_error());

        return $resource;
}

function timespan_output($start, $end, $divid) {
	$time = $end - $start;

//	echo '<div id="' . $divid . '"><p>' . $time . '</p></div>';
	return $time;
}

function timespan_log($start, $end, $module, $routine) {
	$time = $end - $start;

	if(!fwrite(fopen('timespan_log', 'a'), $module . "\t" . $routine . "\t" . $time . "\t" . date('Y-m-d h:i:s') . "\n"))
		die('Timespan Log Error: ' . date('Y-m-d h:i:s'));
}

function metrics_log($resource_local, $ipaddr, $search, $cache_hits, $latency)
{
  if( $resource_local ) {
	$search = mysql_real_escape_string($search);
	mysql_log($resource_local, "INSERT IGNORE INTO metrics VALUES ('$ipaddr', NOW(), '$search', $cache_hits, $latency);");
  }
}

//return id of article name in database
//if article does not exist, create it and return id
function getArticleId($resource_local, $article)
{
  if( $resource_local ) {
	list($id) = mysql_fetch_array(mysql_log($resource_local, "SELECT id FROM titles WHERE article = '$article' LIMIT 1;"));
	
	if (!isset($id))
	{
		mysql_log($resource_local, "INSERT IGNORE INTO titles (article) VALUES ('$article');");
	
		list($id) = mysql_fetch_array(mysql_log($resource_local, "SELECT id FROM titles WHERE article = '$article' LIMIT 1;"));
	}

	return $id;
  }
}

//adds tuple to database for wikipedia term redirect
function addRedirect($resource_local, $original, $redirect)
{
  if( $resource_local ) {
	$original = mysql_real_escape_string($original);
	$redirect = mysql_real_escape_string($redirect);
	mysql_log($resource_local, "INSERT IGNORE INTO redirects VALUES ('$original', '$redirect');");
  }
}

//attempt to cache article contents givennormalized article name
//if article exists in cache, update contets and timestamp
function cacheArticle($resource_local, $title, $contents)
{
  if( $resource_local ) {  
	$id = getArticleId($resource_local, $title);
	
	$contents = mysql_real_escape_string($contents);
	mysql_log($resource_local, "INSERT IGNORE INTO articles VALUES ($id, '$contents', NOW());");
	
	if (!mysql_affected_rows())
		mysql_log($resource_local, "UPDATE articles SET article = '$contents', timestamp = NOW() WHERE id = $id;");

	return mysql_affected_rows();
  }
}

//return article contents given normalized article name
//if article doesn't exist, return null
function checkArticleCache($resource_local, $article)
{
  if( $resource_local ) {
	$contents = null;
	$id = getArticleId($resource_local, $article);
	list($contents) = mysql_fetch_array(mysql_log($resource_local, "SELECT article FROM articles WHERE id = $id;"));

	return $contents;
  }
}

function connectToDatabase()
{
	global $db_config;	

	$resource_local = @mysql_connect($db_config['db_host'], $db_config['db_username'], $db_config['db_password']);

	if ($resource_local)
	{
		ini_set('default_charset', 'UTF-8');
	
		mysql_set_charset('utf8', $resource_local);
	
		@mysql_select_db($db_config['db_name'], $resource_local)
			or die(mysql_error());
	}
	
	return $resource_local;
}

?>

